﻿using System.Collections;
using System.ComponentModel;
using System.Windows.Controls;
using Hopscotch.Presentation.Common.Constants;

namespace Hopscotch.Presentation.Common
{
    public class UserControlBase : UserControl, INotifyDataErrorInfo
    {
        public event System.EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

        /// <summary>
        /// True if there are Validation Errors
        /// </summary>
        public virtual bool HasErrors
        {
            get
            {
                return Validation.GetHasError(this);
            }
        }

        /// <summary>
        /// Get the Validation Errors
        /// </summary>
        public virtual IEnumerable GetErrors(string propertyName)
        {
            return null;
        }

        protected void RaiseErrorsChanged(string key)
        {
            var errorsChanged = ErrorsChanged;

            if (errorsChanged != null)
            {
                errorsChanged(this, new DataErrorsChangedEventArgs(key));
            }
        }

        private DataErrorInfo[] dataProviderError;
        protected DataErrorInfo[] DataProviderError
        {
            get
            {
                if (dataProviderError == null)
                {
                    dataProviderError = new[] { new DataErrorInfo(ErrorType.Error, ErrorMessages.DataProviderNotSetErrorMsg) };
                }
                return dataProviderError;
            }
        }

    }
}
